#coding=utf-8
'''
Combining Multiple Mappings into a Single
Mapping

from collections import ChainMap

 a  ChainMap simply keeps a list
of the underlying mappings and redefines common dictionary operations to scan the
list

update()

'''
a = {'x': 1, 'z': 3}
b = {'y': 2, 'z': 4}

from collections import ChainMap
c = ChainMap(a, b)
print(c)
print(c['x'])  # from a
print(c['y'])  # from b
print(c['z'])  # from z

print(len(c))
print(list(c.keys()))
print(list(c.values()))

c['z'] = 10
c['w'] = 40
del c['x']
print(c)
# del c['y']

## Part
values = ChainMap()
values['x'] = 1
print(values)
# Add a new mapping
values = values.new_child()
values['x'] = 2
print(values)
# Add a new mapping
values = values.new_child()
values['x'] = 3
print(values)

print(values['x'])
# Discard last mapping
values = values.parents
print(values['x'])

# Discard last mapping
values = values.parents
print(values['x'])

print(values)

# Part 4 update()
a = {'x': 1, 'z': 3}
b = {'y': 2, 'z': 4}
print(dict(b))
merged = dict(b)
merged.update(a)
print(merged)
merged['x']